package itheima.zuiye.Test;

public class Sender implements Runnable {
    private static int giftCount = 100;
    private static int xiaohongCount = 0;
    private static int xiaomingCount = 0;

    private String name;

    public Sender(String name) {
        this.name = name;
    }

    @Override
    public void run() {
        while (true) {
            sendGift();
        }
    }

    private synchronized void sendGift() {

        if (giftCount < 10) {
            return;
        }


        giftCount--;
        if ("小红".equals(name)) {
            xiaohongCount++;
        } else if ("小明".equals(name)) {
            xiaomingCount++;
        }


        System.out.println(name + " 送出一份礼品，剩余 " + giftCount + " 份礼品");


        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

